package com.example.communist_app_api.controller.web;

import com.example.communist_app_api.config.mq.NoticeTask;
import com.example.communist_app_api.db.dto.ActivityDTO;
import com.example.communist_app_api.db.dto.MessageDTO;
import com.example.communist_app_api.db.pojo.Message;
import com.example.communist_app_api.exception.GlobalException;
import com.example.communist_app_api.service.MessageService;
import com.example.communist_app_api.service.UserService;
import com.example.communist_app_api.util.JwtUtils;
import com.example.communist_app_api.util.R;
import com.example.communist_app_api.vo.uniapp.req.UniAppCreatMessageReq;
import com.example.communist_app_api.vo.uniapp.req.UniAppCreatSonMessageReq;
import com.example.communist_app_api.vo.uniapp.req.UniAppSelectByConditionReq;
import com.example.communist_app_api.vo.web.req.CreatSonMessageReq;
import com.example.communist_app_api.vo.web.req.DeleteMessageReq;
import com.example.communist_app_api.vo.web.resp.SensitiveWordApiResp;
import com.github.pagehelper.PageInfo;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * @author Brother Sheng Editor
 * @version 1.0
 * Create by 2023/3/4 17:45
 */
@RestController
@RequestMapping("/message")
@Api("管理建言献策板块")
//web端
public class MessageController {
    @Autowired
    private MessageService messageService;

    @Autowired
    private UserService userService;

    @Autowired
    private NoticeTask noticeTask;


    @PostMapping("/selectMessage")
    @ApiOperation("查询留言")
    public R selectMessage( @Valid @RequestBody UniAppSelectByConditionReq req ,HttpServletRequest httpServletRequest){
        Integer userId = getUserIdByWeb(httpServletRequest);
        //可提供模糊查找 ,查询只查留言，不查子留言
        PageInfo<MessageDTO> pageInfo = messageService.selectMessageByConditionByWeb(req.getContent(),req.getStartTime(),req.getEndTime(),req.getPageSize(),req.getPageNum(),userId);
        return R.ok("查询成功").put("pageInfo",pageInfo);
    }

    @PostMapping("/personalMessageCenter/delete")
    @ApiOperation("删除某条评论")
    public R personalMessageCenterDelete(HttpServletRequest httpServletRequest,
                                         @Valid @RequestBody DeleteMessageReq req){
        Integer userIdByWeb = getUserIdByWeb(httpServletRequest);
        messageService.personalMessageCenterDelete(req.getMessageId(),req.getSonMessageId(),userIdByWeb,req.getUserId());
        return R.ok("删除成功");
    }
    //TODO 存在问题当controller层出现报错，noticeTask消息队列和数据库都新增了，需要回滚操作
    @PostMapping("/recallSonMessage")
    @ApiOperation("给留言回复")
    public R createSonMessage(@Valid @RequestBody CreatSonMessageReq req , HttpServletRequest httpServletRequest){
        Integer userIdByWeb = getUserIdByWeb(httpServletRequest);
        Message messageByMessageId = messageService.findMessageByMessageId(req.getMessageId());
        if (messageByMessageId == null) {
            throw new GlobalException("传入的messageId有误，在数据库表中无法查询到");
        }
        //进行留言
        Integer sonMessageId = messageService.createSonMessageBySystem(req.getContent(),req.getMessageId(),userIdByWeb,userIdByWeb);
        //组装发送消息
        Integer receiverId = req.getUserIdByUniApp();
        String noticeName = "建言献策";
        String content = messageService.findContentByMessageId(req.getMessageId());
        String sendContent = messageService.findContentBySonMessageId(sonMessageId);
        String message = "您发布的建言献策收到回复，请查看！原文："+content+"。收到的回复是："+sendContent;

        noticeTask.sendNotice(userIdByWeb, String.valueOf(receiverId),noticeName,message);
        return R.ok("新增子回复");
    }

    private Integer getUserIdByWeb(HttpServletRequest httpServletRequest){
        String token = httpServletRequest.getHeader("token");
        Claims claims = JwtUtils.parseJWT(token);
        String userName = claims.getSubject();
        Integer userIdByWeb = userService.getUserInfo(userName).getId();
        return userIdByWeb;
    }

}
